package com.fzrskj.cloud.free.core.tool;

import ms.core.tool.PropTool;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CObjectTool {

	public static Object[] objectArrAddValue(Object[] oldParam, Object val){
		ArrayList List = new ArrayList();
		for(Object obj : oldParam){
			List.add(obj);
		}
		List.add(val);
		Object[] newParam = List.toArray(); //正确
		return newParam;
	}

	public static void clearMapAgentId(Map<String, Object> cdt){
		String kn = "agentId";
		cdt.remove(kn);
		claerMapKey(cdt, "." + kn);
	}

	public static void claerMapKey(Map<String, Object> cdt, String kn){
		Map<String,Object> newCdt = new HashMap<>();
		for(Map.Entry<String, Object> entry : cdt.entrySet()){
			if (entry.getKey().indexOf(kn) == -1){
				newCdt.put(entry.getKey(), entry.getValue());
			}
		}
		cdt.clear();
		cdt .putAll(newCdt);
	}

	/**
	 * 图片转Base64
	 *
	 * @return
	 */
	public static String getImgToBASE64(String imgFile) {
		String filepath = getFileRootPate() + imgFile;
		filepath = filepath.replace("\\", "/");
		filepath = filepath.replace("//", "/");
		return GetImageStr(filepath);
	}

	/**
	 * 获取图片根路径
	 *
	 * @return
	 */
	public static String getFileRootPate() {
		PropTool cfg = new PropTool("/config/system.properties");
		String staticPath = cfg.getValue("staticPath", "");
		cfg = null;
		return staticPath;
	}

	/**
	 * 读取图片文件，并BASE64加密
	 *
	 * @param imgFile
	 * @return
	 */
	public static String GetImageStr(String imgFile) {
		//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		InputStream in = null;
		byte[] data = null;
		//读取图片字节数组
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);//返回Base64编码过的字节数组字符串
	}

	public static void main(String[] args){
		Map <String,Object> map = new HashMap<>();
		map.put("agentId", "棕色");
		map.put("agentId22", "棕色");
		map.put("two", 2);
		map.put("a.agentId", "棕色");
		map.put("22two", 2);
		map.put("b.agentId", "棕色");
		map.put("33two", 2);
		clearMapAgentId(map);
		System.out.println(map);
	}
}
